/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.nhc.model;

import com.nhc.database.dbmodel.WayPoint;

import zuo.biao.library.base.BaseModel;
import zuo.biao.library.util.JSON;

public class NoteInfo extends BaseModel {

    private static final long serialVersionUID = 1L;


    public String note = "";
    public String strLL = "";//如果编辑的是大地坐标则使用这个坐标
    public int coord_mode = 0;//编辑大地坐标时，坐标的类型
    public int coord_format = 2;

    /**
     * 默认构造方法，JSON等解析时必须要有
     */
    public NoteInfo() {
        //default
        reset();
    }

    public void reset() {
        note = "";
        strLL = "";
        coord_mode = 0;
        coord_format = 2;
    }

    public String toJSONString() {
        return JSON.toJSONString(this);
    }

    public void parseJson(String resultJson) {
        NoteInfo user = null;
        try {
            com.alibaba.fastjson.JSONObject data = JSON.parseObject(resultJson);
            if (data != null) {
                user = JSON.parseObject(data, NoteInfo.class);
            }
        } catch (Exception e1) {

        }
        if (user == null) {
            this.note = resultJson;
        } else {
            this.note = user.note;
            this.strLL = user.strLL;
            this.coord_mode = user.coord_mode;
            this.coord_format = user.coord_format;
        }
    }

    //将大地坐标写入note；或修改真实note后保存
    public void writeWtNote(WayPoint wt) {
        if (wt == null) {
            return;
        }
        note = getWtRealNote(wt);
        strLL = wt.strLL;
        coord_mode = wt.coord_mode;
        coord_format = wt.coord_format;
        if (strLL != null && strLL.length() > 1) {
            note = toJSONString();
            if (note == null) {
                note = "";
            }
            wt.setNote(note);
        }
    }

    //数据库航点读note，初始化大地坐标
    public void readWtNote(WayPoint wt) {
        if (wt == null) {
            return;
        }
        parseJson(wt.getNote());
        wt.setNote(note);
        if (strLL == null || strLL.length() < 1) {
            return;
        }
        wt.strLL = strLL;
        wt.coord_mode = coord_mode;
        wt.coord_format = coord_format;
    }

    //写gpx时或显示时，获取真实的note---其实不需要
    public String getWtRealNote(WayPoint wt) {
        if (wt == null) {
            return "";
        }
        parseJson(wt.getNote());
        if (note == null) {
            return "";
        }
        return note;
    }

    @Override
    protected boolean isCorrect() {//根据自己的需求决定，也可以直接 return true
        return id > 0;
    }

}
